// pages/details/details.js
let {getpubliclsit} = require("../../api/details.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgs:[
            "/images/banner01.jpg",
            "/images/banner02.jpg",
            "/images/banner03.jpg",
            "/images/banner04.jpg",
        ],
        num:0,//商品数量
        goods:{
            
        },
        type:1,
        collist:[],
    },
    goCar(options){
        // console.log("123")
        wx.switchTab({
            url:'/pages/car/car',
        })
    },
    goHome(options){ 
        wx.switchTab({
            url: '/pages/home/home',
        })
    },
    goPay(options){
        wx.navigateTo({
            url:'/pages/pay/pay'
        })
    },
    getcolit(options){
        let collists = wx.getStorageSync("collist");//获取数据
        console.log("collists",collists)
        if(this.data.goods.type==1){
           this.data.goods.type=2;
           this.setData({
               type:this.data.goods.type
           })
           if(collists){
            let index = collists.findIndex(item=>item == this.data.goods.id);
                if(index != -1){
                    return;
                }else{
                    collists.push(this.data.goods.id)
                    wx.setStorageSync('collist', collists);
                }
           }else{
              wx.setStorageSync('collist', [this.data.goods.id]);
           }
        }else if(this.data.goods.type==2){
            this.data.goods.type=1;
            this.setData({
                type:this.data.goods.type
            })
            if(collists){
                let index = collists.findIndex(item=>item == this.data.goods.id);
                if(index != -1){
                    collists.splice(index,1);
                    wx.setStorageSync('collist', collists);
                    //console.log("type1",collists)
                }

            }
                  
               
            // wx.setStorageSync('collist', collists);
        }
    },
    //加入购物车,
    setCart(options){
        console.log(options);
        //数据结构
        // let indexId = this.data.goods.id ;
        // indexId +=1;
        let cartLists ={
            id:this.data.goods.id,
            goodsImage:this.data.goods.goodsImage,
            goodsName:this.data.goods.goodsName,
            goodsPrice:this.data.goods.goodsPrice,
            goodsAddress:"广州",
            num:1
        };
        // 1.判断数据缓存中是否存在
        let goodsCarList = wx.getStorageSync("goodsCarList");//获取数据
        if(goodsCarList){
            // 2.有数据.
            let index = goodsCarList.findIndex(item=>item.id == this.data.goods.id);
                if(index != -1){ //有值返回下标，没有值返回-1
                    // 2.1 判断是否有相同数据，数量加一
                    goodsCarList[index].num += 1;
                }else{
                    // 2.2 没有相同数据，添加数据
                    goodsCarList.push(cartLists);
                }
                // 更新数据
                wx.setStorageSync('goodsCarList', goodsCarList)
        }else{
            // 3.没有数据，直接添加数据
            wx.setStorageSync('goodsCarList', [cartLists]);
        }   
        // 更新商品数量
        this.setData({
            num:wx.getStorageSync('goodsCarList').length
        })
    },
    //请求数据
    getinformation(options){
        let collists = wx.getStorageSync("collist");//获取数据
        if(collists){
          let index = collists.findIndex(item=>item == options.id)
            if(index != -1){
                getpubliclsit({goods_id:options.id}).then((data)=>{
                    // let lsitsadd = data.message ;
                    let lsitsadd = new Set([data.message])
                        for (let e of lsitsadd) {
                            // console.log(e);
                                let {goods_id,goods_small_logo,pics,goods_price,goods_introduce,goods_name} =e;
                                let ldata={
                                    id:goods_id,
                                    goodsImage:goods_small_logo,
                                    goodsName:goods_name,
                                    goodsImgs:pics,
                                    goodsAddress:"广州",
                                    goodsPrice:goods_price-100,
                                    goodsPriceOld:goods_price,
                                    goodsDetails:goods_introduce,
                                    type:2
                                }
                                this.setData({
                                    goods:ldata,
                                    type:ldata.type
                                })
                        }
                })
            }else{
                getpubliclsit({goods_id:options.id}).then((data)=>{
                    // let lsitsadd = data.message ;
                    let lsitsadd = new Set([data.message])
                        for (let e of lsitsadd) {
                            // console.log(e);
                                let {goods_id,goods_small_logo,pics,goods_price,goods_introduce,goods_name} =e;
                                let ldata={
                                    id:goods_id,
                                    goodsImage:goods_small_logo,
                                    goodsName:goods_name,
                                    goodsImgs:pics,
                                    goodsAddress:"广州",
                                    goodsPrice:goods_price-100,
                                    goodsPriceOld:goods_price,
                                    goodsDetails:goods_introduce,
                                    type:1
                                }
                                this.setData({
                                    goods:ldata,
                                    type:ldata.type
                                })
                        }
                })
            }
        }else{
            getpubliclsit({goods_id:options.id}).then((data)=>{
                // let lsitsadd = data.message ;
                let lsitsadd = new Set([data.message])
                    for (let e of lsitsadd) {
                        // console.log(e);
                            let {goods_id,goods_small_logo,pics,goods_price,goods_introduce,goods_name} =e;
                            let ldata={
                                id:goods_id,
                                goodsImage:goods_small_logo,
                                goodsName:goods_name,
                                goodsImgs:pics,
                                goodsAddress:"广州",
                                goodsPrice:goods_price-100,
                                goodsPriceOld:goods_price,
                                goodsDetails:goods_introduce,
                                type:1
                            }
                            this.setData({
                                goods:ldata,
                                type:ldata.type
                            })
                    }
            })
        }
       
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getinformation(options);
        
    },

    /**
     * 
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            num:wx.getStorageSync('goodsCarList').length
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // wx.reLaunch({
        //     url:"/pages/home/home"
        // })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})